package com.wjw.java.others.enum_;

/**
 * 枚举类常用方法：
 */
public class EnumerationMethod {
    public static void main(String[] args) {
        //1.name(),输出的是枚举对象的名字
        Season spring = Season.SPRING;
        System.out.println(spring.name());//SPRING

        /**
         * 2.ordinal(),输出的是该枚举对象的次序/编号,从0开始
         * SPRING("春天","温暖。"),
         * SUMMER("夏天","炎热。"),
         * AUTUMN("秋天","凉爽。"),
         * WINTER("冬天","寒冷。"),
         * WHAT();
         */
        System.out.println(spring.ordinal());//0

        /**
         * 3.values(),返回该枚举类的所有枚举对象数组
         */
        Season[] season2 = Season.values();
        for (Season season : season2) {
            System.out.println(season);
        }

        /**
         * 4.valueOf(),将字符串转为枚举对象,要求字符串为已有的常量名,否则会报错
         */
        Season summer = Season.valueOf("SUMMER");//如果没有SUMMER,就会报错
        System.out.println(summer);

        /**
         * 5.compareTo(),比较的是两个枚举对象的编号的差,如：
         * Season.SPRING.compareTo(Season.AUTUMN)
         * int result = spring.ordinal() - autumn.ordinal()
         * int result = 0 - 2 = -2
         */
        int result = Season.SPRING.compareTo(Season.AUTUMN);
        System.out.println(result);//-2

        //6.循环获取枚举类Week中的每一个枚举对象
        Week[] weeks = Week.values();
        for (Week week : weeks) {
            System.out.println(week.name() + ":"+week.getName());
        }
    }
}
